<?php
declare(strict_types=1);
namespace App\Model\Data\Specific;

use App\Model\Dao\Specific\IFriendServer;
use App\Model\Data\AbstractData;
use Swoft\Db\Eloquent\Model;
use App\Common\Component\RedisComponent;
use Swoft\Bean\Annotation\Mapping\Bean;
use Swoft\Db\Eloquent\Collection;
use App\Model\Entity\ChatFriend;

/**
 * @Bean()
 * @author Administrator
 */
class FriendServerData extends AbstractData implements IFriendServer
{

    use RedisComponent;
    
    public function getModel(): Model
    {
        return ChatFriend::new();
    }
    
    /**
     * 添加朋友
     * @param array $data
     * @return bool
     */
    public function addFriend(array $data): int
    {
        $currentUserId = $this->redis->get($data['token']);
        
        $friend = $data['friend_id'];
        
        return $this->add([
            'user_id' => $currentUserId,
            'friend_id' => $friend
        ]);
    }
    
    public function addFriendStatus(array $data): bool
    {
        return 0 !== $this->addFriend($data);
    }
    
    /**
     * 获取 好友列表
     * @return Collection
     */
    public function getFriendIds(array $data): Collection
    {
        return $this->getModel()->where('user_id', '=', $this->redis->get($data['token']))->get(['friend_id']);
    }
    
}

